package com.zhengjiang.log4j2.demo.filter;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @ClassName LogFilter
 * @Description
 * @Author zhengjiang(99514925 @ qq.com)
 * @Date 2020-06-24 14:04
 * @Version V1.0
 **/
@Slf4j
@WebFilter(filterName = "logFilter",urlPatterns = "/*")
@Order(1)
public class LogFilter implements Filter {

    @Value("${spring.application.name}")
    private String serverName;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("logFilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        MDC.put("ip",request.getRemoteAddr());
        MDC.put("serverName",serverName);
        filterChain.doFilter(servletRequest,servletResponse);
    }



}
